简体   繁体   中英

how to update a dataset in R shiny

Can I get help on how to get the below code to work? I am looking to update the numbers in

dataExample <- getDataset(n1 = c(20),n2 = c(20), means1 = c(18), means2 = c(9),stds1 = c(14), stds2 = c(14))

based on input values and then generate the plot whenever I apply changes but not sure how to do it. I read it somewhere it says need to use reactive or observe but I am not sure how to modify the code?

library(shiny)
library(plyr)
library(rmarkdown)
library(rpact)

design <- getDesignGroupSequential(kMax = 2, alpha = 0.05, informationRates = c(0.5, 1), typeOfDesign = "WT", deltaWT = 0.5)

ui <- fluidPage(
  titlePanel("Efficacy Monitoring Using Conditional Power"),
  sidebarLayout(
    sidebarPanel(
      numericInput('nn1', 'Number of Subjects at Current stage (Active)', 20, min = 1, max = 100),
      numericInput('nn2', 'Number of Subjects at Current stage (Control)', 20, min = 1, max = 100),
      textInput('Mean1', 'Mean1',"18"),
      textInput('Mean2', 'Mean2',"9"),
      textInput('SD1', 'SD1',"14"),
      textInput('SD2', 'SD2',"14"),
      textInput('nPlanned', 'Additional Numbers Planned',"40"),
      submitButton(text = 'Apply Changes')
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(plotOutput("plot2")
    )
  )
)

server <- function(input,output){
  
  dataExample <- getDataset(
    n1     = c(20),
    n2     = c(20), 
    means1 = c(18),
    means2 = c(9),
    stds1  = c(14),
    stds2  = c(14)
  )
  
  stageResults <- getStageResults(design, dataExample,thetaH0 = 0)
  
  output$plot2<-renderPlot(
    plot(stageResults, nPlanned = c(as.numeric(input$nPlanned)), thetaRange = c(0, 20))
  )
}

# Run the app ----
shinyApp(ui, server)

Use a reactive function and put the input values

library(shiny)
library(plyr)
library(rmarkdown)
library(rpact)

design <- getDesignGroupSequential(kMax = 2, alpha = 0.05, informationRates = c(0.5, 1), typeOfDesign = "WT", deltaWT = 0.5)

ui <- fluidPage(
  titlePanel("Efficacy Monitoring Using Conditional Power"),
  sidebarLayout(
    sidebarPanel(
      numericInput('nn1', 'Number of Subjects at Current stage (Active)', 20, min = 1, max = 100),
      numericInput('nn2', 'Number of Subjects at Current stage (Control)', 20, min = 1, max = 100),
      # WHY THE CHOICE OF textInput instead of numericInput ?
      textInput('Mean1', 'Mean1',"18"),
      textInput('Mean2', 'Mean2',"9"),
      textInput('SD1', 'SD1',"14"),
      textInput('SD2', 'SD2',"14"),
      textInput('nPlanned', 'Additional Numbers Planned',"40"),
      submitButton(text = 'Apply Changes')
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(plotOutput("plot2")
    )
  )
)

server <- function(input,output){
  # Use the input values in a reactive function
  dataExample <- reactive({
    getDataset(
      n1     = input$nn1,
      n2     = input$nn2,
      means1 = as.numeric(input$Mean1),
      means2 = as.numeric(input$Mean2),
      stds1  = as.numeric(input$SD1),
      stds2  = as.numeric(input$SD2)
    )
  })
  
  output$plot2<-renderPlot({
    stageResults <- getStageResults(design, dataExample(), thetaH0 = 0)
    plot(stageResults, nPlanned = c(as.numeric(input$nPlanned)), thetaRange = c(0, 20))
  })
}

# Run the app ----
shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM