dimanche 21 juin 2020

Dynamic Graphs not getting displayed properly in Shiny R?

I am trying to create a Shiny App. In this particular app i have a set of Radio buttons where choosing one will display a set of options below as Check Boxes and Choosing another radio button will choose other set of options and clicking on the checkboxes will generated graphs. Kindly find the UI and the Server Code below.

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
d <-
  data.frame(
    year = c(1995, 1995, 1995, 1996, 1996, 1996, 1997, 1997, 1997),
    Product_Name = c(
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed",
      "Table",
      "Chair",
      "Bed"
    ),
    Product_desc = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z"),
    Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6)
  )

ui <- shinyUI(fluidPage(
  useShinydashboard(),
  tabPanel(
    "Plot",
    sidebarLayout(
      sidebarPanel(
        radioButtons(
          "Choose",
          "Choose One",
          c("Year" = "p", "Numbers" = "l")
        ),
        uiOutput('checkbox'),
        #width = 2,
        position = "bottom"),
      mainPanel(uiOutput("graph"),
                uiOutput("graph_1"))
      
    )
  )
))

server <- function(input, output, session) {
  
  z_1 <- reactiveValues(years = NULL)
  z_2 <- reactiveValues(numbers = NULL)
  
  observeEvent(input$X, {
    z_1$years <- input$X
  })
  
  observeEvent(input$X_1, {
    z_2$numbers <- input$X_1
  })
  
  output$checkbox <- renderUI({
    if (input$Choose == "p") {
      checkboxGroupInput("X",
                         "year",
                         choices = (unique(d$year)),selected = z_1$years)
      
    } else{
      checkboxGroupInput("X_1",
                         "Numbers",
                         choices = c("1","2","3","4"), ,selected = z_2$numbers)
    }
    
  })
  
  output$graph <- renderUI({
    ntabs = length(input$X)
    myTabs = lapply(seq_len(ntabs), function(i) {

        fluidRow(plotOutput(paste0("plot", i)))
    })
  })
  
  
  output$graph_1 <- renderUI({
    ntabs = length(input$X_1)
    myTabs = lapply(seq_len(ntabs), function(i) {

      fluidRow(plotOutput(paste0("plot_1", i)))
    })
  })
  
  
  observe (lapply(length(input$X), function(i) {
    output[[paste0("plot", i)]] <- renderPlot({
      if (length(input$X) > 0) {
        d %>%
          ggplot(aes(Product_Name, Cost)) +
          geom_col(aes(fill = Product_desc),
                   position = position_dodge(preserve = "single")) +
          facet_wrap( ~ input$X[i],
                      scales = "free_x",
                      strip.position = "bottom") +
          theme(strip.placement = "outside") +
          theme_bw()
      }
    })
    
  }))
  
  
  observe (lapply(length(input$X_1), function(i) {
    output[[paste0("plot_1", i)]] <- renderPlot({
      if (length(input$X_1) > 0) {
        d %>%
          ggplot(aes(Product_Name, Cost)) +
          theme(strip.placement = "outside") +
          theme_bw()
      }
    })
    
  }))
  
}

shinyApp(ui, server)

The choices in CheckboxGroupInput() are dynamic in my case. Depending upon the “Years” Radio Button Selection the Checkboxes would get generated. When the checkboxes are selected, corresponding Graphs will be getting generated in the main panel.

The Graph is getting generated, but when I choose the next “Numbers” radio button and select a checkbox under the "Numbers" radio button, the graph is getting generated for that checkbox selection But its below the previous graph that was generated in the previous "Years" radio Button checkbox selection. I am not sure why this particular issue is taking place.

I wanted only those graphs that get generated for the particular radio button selection and its corresponding Checkbox selections. According to the current code the checkbox selections would get retained even when the radio buttons are switched by the users. I don't want any change in this particular functionality please.

Please let me know your suggestions.




How do I pass the values of checked boxes to an array using javascript (not jQuery)?

newbie coder here. I've been looking for a couple hours now and haven't been able to find exactly what I'm looking for (but close!). I am using javascript and HTML to attempt getting the values of checked items into an array, and then display the array (at this point) to check that I've passed values to that array.

I have successfully tested that my .js and .html files are linked, and I can manually define an element in the array and that element will display, but I can't seem to figure out how to get the 'value' of a checked box passed into the array. Currently trying a for loop (found on another post) without luck. Here's a couple lines of the HTML:

<div class=columns id=produceOne>
  <input type="checkbox" name="produceitem" id="apples" value="apples"><label for="apples">Apples</label><br>
  <input type="checkbox" name="produceitem" id="avocados" value="avocados"><label for="avocados">Avocados</label><br>
</div>

And here is the javascript I'm working with so far...

var grocItems = [];
var checkboxes = document.querySelectorAll("input[type=checkbox]:checked");

function sendProduce() {
  for (var i = 0; i < checkboxes.length; i++) {
    grocItems.push(checkboxes[i].value);
  }
  
  alert(grocItems);
}

Thanks for any and all help!




can i set checked or unchecked checkbox reading a string value?

I have a string $str that should value 1 or 0 and a checkbox. When i read the string value i want to set the checkbox checked if $str value is 1 or unchecked if $str value is 0.

<?
    if ($str == 1){
        // checkbox checked                                                     
    } else {
        // checkbox unchecked                                                           
    }
?>  
<input type='checkbox' name='nome_posiz[]' />



I can't see 'on' in url

I am trying to fetch elements which are checked so that I can delete them from database .

I used 'get' method so that I could see elements those I am selecting .

 []21 food
 []22 eggs
 []23 bread
 []24 bread
    

this is my data in database if I am selecting food it is showing

  http://localhost/shopping/delete.php?21=&delete=     (in url) 
    

it should be 21='on', but it is not showing why and please tell me how to fetch them to delete if I cannot do like this .

        <?php require_once("dbconnect.php") ?>
        <?php
        db();
        global $link;
        $query="SELECT * FROM items";
        $result=mysqli_query($link,$query);
        ?>
          <form  action="delete.php" method="get">
            <?php
        while ($arr=mysqli_fetch_array($result)) {
          ?>
    
    
    
      <ul>
        <li> <input type="checkbox" name="<?php echo $arr['id'] ?>" value=""> <?php echo $arr['id'] ?> <?php echo $arr['itemname'] ?> </li>
      
      </ul>
      <?php
       }
       ?>
    
      <button type="submit" name="delete">delete</button>
      </form>



samedi 20 juin 2020

Use binded CheckBox value also outside of the DataContext C# WPF

I want to use the status of a checkbox in another class. Unfortunately, this does not quite work.

Here is my code example: Please note: All Bindings, OnPropertyChanged and the RelayCommand, which has the Icommand, were implemented correctly and work. But not in here, to make it easier to read.

MainWindow XAML:

<CheckBox Content="Select or not" IsChecked="{Binding IsSelected}"/>

<Button Content="Test" Command="{Binding ButtonCommand}">

MainWindowViewModel (DataContext):

private bool _isSelected;
public bool IsSelected
{
    get { return _isSelected; }
    set
    {
        if (_isSelected != value)
        {
            _isSelected = value;
            OnPropertyChanged(nameof(IsSelected));
        }
    }
}


public RelayCommand ButtonCommand { get; private set; }

public HomeViewModel()
{
    InitCommands();
}


private void InitCommands()
{
    ButtonCommand = new RelayCommand(DoJob);
}

private void DoJob(object sender)
{
    Class1 class1 = new Class1();
    class1.Method();
}

Class1:

HomeViewModel viewModel = new HomeViewModel();

public void Method()
{
    if (viewModel.IsSelected)
    {

    }
}

How can I use the status of IsSelected in other classes?

Unfortunately I can't access it directly (I don't know why).

Thanks for your help.




Importing data from json file into Checkbox and get multiple of them when adding new data

What I am trying to do is import my JSON data into the Checkboxes i made and not use text.

As you can see i have now multiple checkboxes but there needs to be only one that fetches the data from my json file to make multiple.

And everytime I add data to the json file a new checkbox needs to appear. I am still new to react native so all help would be appreciated.

import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
import {CheckBox} from "native-base";
import { globalStyles } from '../../../styles/global';
const GLOBAL = require('../../assets/Globals');
const lessondata = ("../../assets/json/" + global.currentLesson + ".json");
import jsondata from "../../assets/json/1.json";

export default class App extends React.Component {
  state={
    selectedLang1:false,
    selectedLang2:false,
    selectedLang3:false,
    selectedLang4:false,

  }

  
  render(){
    const {selectedLang1,selectedLang2,selectedLang3,selectedLang4} = this.state
    return (
      <View style={styles.container}>
        <View style={styles.item} >
            <CheckBox checked={selectedLang1} color="#fc5185" onPress={()=>this.setState({selectedLang1:!selectedLang1})}/>
            <Text style={
              {...styles.checkBoxTxt,
                color:this.state.selectedLang1?"#fc5185":"gray",
                fontWeight:this.state.selectedLang1? "bold" :"normal"
              }}
              >Python</Text>
        </View>
        <View style={styles.item}>
            <CheckBox checked={this.state.selectedLang2} color="#fc5185" onPress={()=>this.setState({selectedLang2:!selectedLang2})}/>
            <Text style={
              {...styles.checkBoxTxt,
                color:this.state.selectedLang2?"#fc5185":"gray",
                fontWeight:this.state.selectedLang2? "bold" :"normal"
              }}
              >Javascript</Text>
        </View>
        <View style={styles.item}>
            <CheckBox checked={this.state.selectedLang3} color="#fc5185" onPress={()=>this.setState({selectedLang3:!selectedLang3})}/>
            <Text style={
              {...styles.checkBoxTxt,
                color:this.state.selectedLang3?"#fc5185":"gray",
                fontWeight:this.state.selectedLang3? "bold" :"normal"
              }}
              >Php</Text>
        </View>
        <View style={styles.item}>
            <CheckBox checked={this.state.selectedLang4} color="#fc5185" onPress={()=>this.setState({selectedLang4:!selectedLang4})}/>
            <Text style={
              {...styles.checkBoxTxt,
                color:this.state.selectedLang4?"#fc5185":"gray",
                fontWeight:this.state.selectedLang4? "bold" :"normal"
              }}
              >C#</Text>
        </View>
        <TouchableOpacity style={styles.submit}>
          <Text style=>SUBMIT</Text>
        </TouchableOpacity>
  
  
      </View>
    );
  }
  
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f6f6f6',
    alignItems: 'center',
    justifyContent: 'center',
  },
  header:{
    fontSize:25,
    fontWeight:"bold",
    color:"#364f6b",
    marginBottom:40
  },
  item:{
    width:"80%",
    backgroundColor:"#fff",
    borderRadius:20,
    padding:10,
    marginBottom:10,
    flexDirection:"row",
  },
  checkBoxTxt:{
    marginLeft:20
  },
  submit:{
    width:"80%",
    backgroundColor:"#fc5185",
    borderRadius:20,
    padding:10,
    alignItems:"center",
    marginTop:40
  }
});



how prevent unchecked checkbox

This code works fine for me, but I don't need the alert and don't wanna uncheck the checkbox again, I tried change false to true and erase the message but it doesn't work.

$("#test").on('change', function() {
    this.checked=!this.checked?!confirm('Really uncheck this one ?'):true;
});