I want to delete the last row of a table using an action button. I have tried to follow this post Shiny: dynamically add/ remove textInput rows based on index
but I don't know how to apply the idea to my particular case.
A minimal reproducible example
library(shiny)
ui <- fluidPage(
sidebarPanel(numericInput("c1","Example", NA),
actionButton("update", "Update Table"),
br(), br(),
actionButton("reset", "Clear")
),
mainPanel( tableOutput("example")
)
)
server <- function(input, output, session) {
# stores the current data frame, called by values() and set by
values(new_data_table)
values <- reactiveVal(data.frame(A=1, B=2, C=3))
# update values table on button click
observeEvent(input$update,{
old_values <- values()
A_new <- input$c1
B_new <- A_new + 2
C_new <- A_new + B_new
new_values <- data.frame(A=A_new, B=B_new, C=C_new)
# attach the new line to the old data frame here:
new_df <- rbind(old_values, new_values)
#store the result in values variable
values(new_df)
#reset the numeric input to NA
updateNumericInput(session, "c1", "Example", NA)
})
#delete last row
deleteEntry <- observeEvent(input$reset,{
#....
})
#Print the content of values$df
output$example <- renderTable({ return(values()) })
}
shinyApp(ui = ui, server = server)
Actually I don't know how to call the last row of my interactive data frame. I have tried something like values() <- values[nrow(values())-1] but it doesn't work. Any suggestion?
EDITED
Following the suggestion below I have modified the deleteEntry function and now it works.
##delete last row
deleteEntry <- observeEvent(input$reset,{
values( values()[-nrow(values()),])
})
To remove the last row of a data.frame as a reactiveVal , use this syntax:
values(values()[-nrow(values()),])
Related
I'm trying to store user input into a pre-existing dataframe in shiny.
In this simplified example I have a dataframe with a column labelled 'colour' and I want radio choice buttons to be able to assign values to each row in the dataframe, starting with row 1, then row 2 then row 3 etc. But it seems the dataframe always resets to the original value. What am I misunderstanding about shiny here?
library(shiny)
library(tidyverse)
library(DT)
dataframe <- tibble(row_id = c(1,2,3),
colour = c("","",""))
ui <- fluidPage(
radioButtons("colour",
"Pick a colour:",
choices = c("blue","green","red")),
actionButton("next_button", "Pick the next colour"),
DTOutput("dataframe")
)
server <- function(input, output) {
vals <- reactiveValues(active_row = 1)
observeEvent(input$next_button,{
dataframe[vals$active_row, "colour"] = input$colour
# view(dataframe)
# print(vals$active_row)
vals$active_row = vals$active_row + 1
output$dataframe <- renderDataTable(dataframe)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I would store your data also in reactiveValues. In addition, you can move your output outside of your observeEvent. This server function should work based on your description.
server <- function(input, output) {
vals <- reactiveValues(active_row = 1, data = dataframe)
observeEvent(input$next_button,{
vals$data[vals$active_row, "colour"] = input$colour
vals$active_row = vals$active_row + 1
})
output$dataframe <- renderDataTable(vals$data)
}
I am trying to build a shiny app with rhandsontable in it. This rhandsontable is based on the datframe I create inside the app.
In the app I initially display the first row of this dataframe with al 3 columns. When the value of the 1st column is modified by the list of its dropdown levels and press search then the other 2 columns are modified.
I would like to do the same with the second column as well. Also I would like initially to display only the first 2 columns and the third will be displayed when the search button is pressed and of course if this row exists.
I tried to replicate what I did for the 1st column (commented code) but it does not work. The first 2 columns should always display all of their levels in the dropdown but the third only the available ones after the every search.
DF = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
transmission=factor(rep(c("automatic","manual"),5)))
write.csv(DF,"C:/Users/User/Documents/Test//cars.csv", row.names = FALSE)
ui.r
library(shiny)
library(rhandsontable)
ui <- fluidPage(
titlePanel("RHandsontable"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv"),
actionButton("sr", "Search")
),
mainPanel(
rHandsontableOutput("test")
)
)
)
server.r
server <- function(input, output) {
# Assign value of 12345 as default to postcode for the default table rendering
values <- reactiveValues(postcode = "12345"
#car_group = "Microcar"
,tabledata = data.frame())
# An observer which will check the value assigned to postcode variable and create the sample dataframe
observeEvent(values$postcode,{
inFile <- input$file1
if (is.null(inFile))
return(NULL)
DF<- read.csv(inFile$datapath,stringsAsFactors = T)
for(i in 1:ncol(DF)){
DF[,i]<-as.factor(DF[,i])
}
DF
DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
transmission=factor(rep(c("automatic","manual"),5)))
# Created dataframe is assigned to a reactive dataframe 'tabledata'
values$tabledata <- DF2[ which(DF2$agency_postcode ==values$postcode
#&DF2$car_group==values$car_group
), ]
for(i in 2:ncol(values$tabledata)){
values$tabledata[,i] <- factor(values$tabledata[,i])
}
})
# Capture changes made in the first column of table and assign the value to the postcode reactive variable. This would then trigger the previous observer
observeEvent(input$test$changes$changes,{
col <- input$test$changes$changes[[1]][[2]]
if(col==0){
values$postcode <- input$test$changes$changes[[1]][[4]]
#values$car_group<-input$test$changes$changes[[1]][[4]]
}
})
# Use the reactive df 'tabledata' to render.
output$test <- renderRHandsontable({input$sr
isolate(rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
hot_col(colnames(values$tabledata)))
})
}
In the code that you have added for retrieving the value selected in second column, we would need to update something.
if(col==0){
values$postcode <- input$test$changes$changes[[1]][[4]]
#values$car_group<-input$test$changes$changes[[1]][[4]]
}
Index of handsontable starts with 0. So, its 0 for first column and 1 for second column, meaning you cannot update the values to car_group reactive variable within the if condition for the first column
A solution to your current question based on the answer that I provided here. Update rhandsontable by changing one cell value
library(shiny)
library(rhandsontable)
ui <- fluidPage(
titlePanel("RHandsontable"),
sidebarLayout(
sidebarPanel(),
mainPanel(
rHandsontableOutput("test")
)
)
)
server <- function(input, output) {
# Assigning blank values to reactive variable as all the values need to be listed first
values <- reactiveValues(postcode = "",cargroup = "",tabledata = data.frame())
observeEvent(values$postcode,{
DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
transmission=factor(rep(c("automatic","manual"),5)))
# When the user selects any value from the dropdown, filter the table and update the value of reactive df
if(values$postcode!=""){
values$tabledata <- DF2[ which(DF2$agency_postcode ==values$postcode), ]
}else{
# When the postcode value is blank, meaning the user hasn't selected any, the table
# will render without the third column
values$tabledata <- DF2[,-3]
}
})
observeEvent(values$cargroup,{
DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
transmission=factor(rep(c("automatic","manual"),5)))
values$tabledata <- DF2
# When the user selects any value from the dropdown, filter the table and update the value of reactive df
if(values$cargroup!=""){
values$tabledata <- DF2[ which(DF2$car_group ==values$cargroup), ]
}else{
# When the cargroup value is blank, meaning the user hasn't selected any, the table
# will render without the third column
values$tabledata <- DF2[,-3]
}
})
# Observer for changes made to the hot
observeEvent(input$test$changes$changes,{
col <- input$test$changes$changes[[1]][[2]]
# Changes made in first column
if(col==0){
values$postcode <- input$test$changes$changes[[1]][[4]]
}
# Changes made in second column
if(col==1){
values$cargroup <- input$test$changes$changes[[1]][[4]]
}
})
# Render the hot object
output$test <- renderRHandsontable({
rhandsontable(values$tabledata[1,], rowHeaders = NULL, width = 550, height = 300)%>%
hot_col(colnames(values$tabledata))
})
}
shinyApp(ui = ui, server = server)
Check if this suits your needs. You can then update the observer part based on search button instead of being reactive to the changes made by user.
I am trying to create a shiny app that has a rhandsontable in it. I want rhandsontable to be able to update its values in one of its columns if the corresponding values in another column is selected/ checked. So far, I have been able to use reactive / observe events to change the output values between two objects but i am unable to wrap my head around it , i.e, how do i make once column of rhandsontable reactive to another column in the same table ?
Here is a simple example of what i am trying to build:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
rHandsontableOutput('table')
)
server <- function(input,output,session)({
data <- data.frame(c1=c(5,10,15), c2=c(3,6,9) , diff=c(0,0,0), select= as.logical( c(FALSE,FALSE,FALSE)))
output$table <- renderRHandsontable({
rhandsontable(data)
})
})
shinyApp(ui = ui, server = server)
So if i check the column 'Select', column 'diff' should produce the difference between column c1 & c2
From what I understand, your goal is to do some calculation depending on the values of some other column. So if for example a box of the third column is checked, you might want to compute the difference between elements of column 1 and 2.
If you had just a data frame, that would be easy, wouldn't it? Well, this is possible using reactive values. The main idea is that you can store the rhandsontable in a data frame in the backend, modify the data frame and then render the modified data frame once again back in the handsontable.
I hope this helps:
For a more detailed example on reactive values you can see
this: http://stla.github.io/stlapblog/posts/shiny_editTable.html
and this : https://www.youtube.com/watch?v=BzE1JmC0F6Q&list=PL6wLL_RojB5wXR3NR3K38sIvexZ_45alY&index=3
library(rhandsontable)
library(shiny)
ui <- fluidPage(
mainPanel(
rHandsontableOutput("hot")
)
)
server = function(input, output, session){
df<- data.frame(c1=c(5,10,15), c2=c(3,6,9) , diff=c(0,0,0), select= as.logical( c(FALSE,FALSE,FALSE)))
values <- reactiveValues(data = df)
observe({
if(!is.null(input$hot)){
values$data <- as.data.frame(hot_to_r(input$hot))
isolate(values$data[,'diff'] <- ifelse(values$data[,'select'], values$data[,'c1']-values$data[,'c2'] ,0))
print(values$data)
output$hot <- renderRHandsontable({
rhandsontable(values$data)
})
}
})
output$hot <- renderRHandsontable({
rhandsontable(values$data)
})
}
shinyApp(ui, server)
I'm building a shiny app that queries an SQL database so the user can ggplot the data. I would like the user to be able to rename factors manually but am struggling to get going. Here is an example of what I want to do:
ui.R
library(markdown)
shinyUI(fluidPage(
titlePanel("Reactive factor label"),
sidebarLayout(
sidebarPanel(
numericInput("wafer", label = h3("Input wafer ID:"), value = NULL),
actionButton("do", "Search wafer"),
textInput("text", label = h3("Factor name to change"), value = ""),
textInput("text", label = h3("New factor name"), value = ""),
actionButton("do2", "Change name")
),
mainPanel(
verbatimTextOutput("waf"),
verbatimTextOutput("que"),
verbatimTextOutput("pos"),
dataTableOutput(outputId="tstat")
)
)
)
)
server.R
# Create example data
Name <- factor(c("Happy", "New", "Year"))
Id <- 1:3
dd <- data.frame(Id, Name)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "dd", dd)
query <- function(...) dbGetQuery(con, ...)
wq = data.frame()
sq = data.frame()
shinyServer(function(input, output){
# create data frame to store reactive data set from query
values <- reactiveValues()
values$df <- data.frame()
# Wait for user to search
d <- eventReactive(input$do, { input$wafer })
# Reactive SQL query
a <- reactive({ paste0("Select * from dd where Id=",d()) })
wq <- reactive({ query( a() ) })
# Check outputs
output$waf <- renderPrint(input$wafer)
output$que <- renderPrint({ a() })
output$pos <- renderPrint( wq()[1,1] )
# observe d() so that data is not added until user presses action button
observe({
if (!is.null(d())) {
sq <- reactive({ query( a() ) })
# add query to reactive data frame
values$df <- rbind(isolate(values$df), sq())
}
})
output$tstat <- renderDataTable({
data <- values$df
})
})
In static R I would normally use data table to rename factors i.e.:
DT <- data.table(df)
DT[Name=="Happy", Name:="Sad"]
But I'm not sure how to go about this with a reactiveValues i.e. values$df.
I have read this (R shiny: How to get an reactive data frame updated each time pressing an actionButton without creating a new reactive data frame?). This lead me to try this but it doesn't do anything (even no error):
observeEvent(input$do2, {
DT <- data.table(values$df)
DT[Name == input$text1, Name := input$text2]
values$df <- data.frame(values$df)
})
Perhaps there is a way around this..maybe there is a way to use an action button to "lock in" the data as a new data frame, which can then be used to rename?
Sorry for such a long winded question. My real app is much longer and more complex. I have tried to strip it down.
Your approach works but there are a few issues in your app.
In ui.R, both textInput have the same id, they need to be different so you can refer to them in the server.R. In the observeEvent you posted, you refer to input$text1 and input$text2 so you should change the id of the textInputs to text1 and text2.
In the observeEvent you posted, the last line should be values$df <- as.data.frame(DT), otherwise it does not change anything.
I am building an application in which users can enter data values for table by column. Once ADD button is clicked the entered values would be appended by column to the existing one. e.g.
if col1, 2, 3 are entered and ADD is clicked we have in the display
col1
2
3
and if col2, 4, 7 are entered and ADD clicked we have have the display
col1 col2
2 4
3 7
etc.
I would like it such that when the add button is clicked, the input fields are cleared to allow for the entry of new column. Please find below codes for the ui and server. The output table also does not display properly, any assistance to solve this problem too would be appreciated.
ui.R
shinyUI(pageWithSidebar(
headerPanel("My data table"),
sidebarPanel(h5("Enter input"),
textInput("colname","Enter Column Name",NA),
numericInput("x","X",NA),
numericInput("y","Y",NA),
br(),
actionButton("Add","ADD")),
mainPanel(verbatimTextOutput("out"))
))
And
server.R
shinyServer(function(input,output){
myTable <- reactive({
if(input$Add > 0)
return(isolate({
colnm <- input$colname
x <- input$x
y <- input$y
result <- data.frame(rbind(colnm,x,y))
result
}))
})
output$out <- renderTable({
myTable()
})
})
The table needs to be rendered using renderTable rather then verbatimTextOutput. I guess you want to keep old inputs. One way to do this would be to use reactiveValues. EDIT: I didnt see you wanted to reset inputs. To reset inputs use the updateNumericInput and updateTextInput function. You will also need to pass a session variable inot your server function.
runApp(
list(ui = pageWithSidebar(
headerPanel("My data table"),
sidebarPanel(h5("Enter input"),
textInput("colname","Enter Column Name",NA),
numericInput("x","X",NA),
numericInput("y","Y",NA),
br(),
actionButton("Add","ADD")),
mainPanel(tableOutput("out"))
),
server = function(input,output,session){
myValues <- reactiveValues()
observe({
if(input$Add > 0){
isolate({
colnm <- input$colname
x <- input$x
y <- input$y
if(!is.null(myValues$myDf)){
myValues$myDf <- cbind(myValues$myDf,
data.frame(setNames(list(c(x, y)), colnm))
)
}else{
myValues$myDf <- data.frame(setNames(list(c(x, y)), colnm))
}
})
updateNumericInput(session, "x","X", NA)
updateNumericInput(session, "y","Y", NA)
updateTextInput(session, "colname","Enter Column Name",NA)
}
})
output$out <- renderTable({
myValues$myDf
})
})
)
EDIT:
You could change to
updateNumericInput(session, "x","X", 3)
updateNumericInput(session, "y","Y", 5)
updateTextInput(session, "colname","Enter Column Name",'Default NAME')
and it works. Now the values change to default values of 3,5 and 'Default NAME'