How to change the value of a function depending on the radio button choice?

19 views
Skip to first unread message

Anastasia Semenova

unread,
Jan 10, 2017, 2:23:46 PM1/10/17
to Shiny - Web Framework for R

I`d like to multiply the value of function fuz <- gset_defuzzify by 2 if user choose English on radio button and by 1,5 if Spanish is chosen.
Thanks

P.S.: I still don`t know how to take input values for fuzzy_inference...
library(shiny)
    library(shinyjs)
    library(sets)
    library(datasets)

    ui <- fluidPage(numericInput(inputId = "one", label="Type number",1, min=1, max=120),
    numericInput(inputId = "two", label="Type number",1, min = 1, max=120),
            numericInput(inputId = "three", label="Type number",1, min = 1, max=120),
            numericInput(inputId = "four", label="Type number",1, min = 1, max=120),
            textOutput("sub"), br(),
            actionButton("act", "Show"),

            br(),

            radioButtons(inputId = "RB",label="Choose", 
                         c("English"="English",
                           "French"="French",
                           "German"="German",
                           "Spanish"="Spanish",
                           "None"="None")),
            textOutput("sw"),
            textOutput("text")


    )
    server <- function(input, output){
    output$text <- renderText({
RB <- switch(input$RB,
             English= "You schould learn German.",
             French = "You schould learn Spanish.",
             German = "You schould learn English.",
             Spanish = "You schould learn Portuguese.",
             None ="You schould learn Polish.")
    })
    variables <- set(wo = fuzzy_partition(varnames = c(notMany2 = 15, enough2 = 25, many2 = 35),FUN = fuzzy_cone, radius = 10),
               top = fuzzy_partition(varnames = c(notMany3 = 20, enough3 =   50, many3 = 100),FUN = fuzzy_cone, radius = 25),
               c = fuzzy_partition(varnames = c(k4 = 52, k3 = 42, k2 = 32,k1 = 22), sd = 3)
    )
    rules <- set (fuzzy_rule(wo %is% notMany2 && top %is% notMany3 
                       || wo %is% notMany2 && top %is% notMany3 
                       || wo %is% notMany2 && top %is% notMany3 
                       , c %is% k1),
            fuzzy_rule( wo %is% notMany2 && top %is% enough3 
                        || wo %is% notMany2 && top %is% many3,c %is% k2))
    system <- fuzzy_system(variables,rules)

    fi <- fuzzy_inference(system, list(wo = 20, top= 10))

      fuz <- gset_defuzzify(fi, 'centroid')

    output$sub <- renderText({fuz})


    }


    shinyApp(ui = ui, server = server)

Atul Sharma

unread,
Jan 10, 2017, 6:05:55 PM1/10/17
to Shiny - Web Framework for R
Hi, Anastasia: I'm not sure I understand the question completely, but your call to set the values of the radio button should be 
choices = c("English", "French", "German", "Spanish", "None") for the 3rd argument. 

Then when you use the output on the server side, the switch statement should be assign a value to a variable i.e. 

RB = switch(input$RB, "English" = "You Should Learn German", "French" = "You Should Learn Spanish", ....)

You can then work with the new value of RB namely "You Should ..."

Atul

Anastasia Semenova

unread,
Jan 10, 2017, 6:14:18 PM1/10/17
to Shiny - Web Framework for R
Thanks for your respond. I'd like to do this:
If user choose English the value (which is kept in fuz) should be multiplied by a number (for example 2).
Message has been deleted

Anastasia Semenova

unread,
Jan 11, 2017, 6:48:22 AM1/11/17
to Shiny - Web Framework for R
So I tried this: 

output$sub <- renderText({fuz})
  z <- reactive(if(output$sub=="You schould learn German") (fuz*2) else 0)
  output$sw <- renderText({z()})

But it gave me an error: Reading objects from shinyoutput object not allowed.


Anastasia Semenova

unread,
Jan 11, 2017, 10:00:29 AM1/11/17
to Shiny - Web Framework for R
The answer is z <- reactive(if(input$RB=="English") (fuz*2))


Reply all
Reply to author
Forward
0 new messages