Select input from a list in Shiny - r

I am new to Shiny R. Based on a Table Editor, I would like to customise certain functions but I am having some issues. There is an Add button that contains several fields. I would not like to write every time some fields to fill the form, e.g., the Supplier or product names. I tried using SelectInput from the csv file created by the app, but when clicking the add row button, there is a drop list of the Suppliers but I can't write any other name that is not in that list. I read that another option would be textInput.typeahead but I can't figure out how to make it work.
Here is the piece of code I am using:
mydata1 = read.csv("Supplier_NCR1.csv", row.names = NULL, na.strings = "", stringsAsFactors = FALSE)
mylist1 <- as.list(unique(mydata1[[4]]))
observeEvent(input$Add_row_head, {
### This is the pop up board for input a new row
showModal(modalDialog(title = "Add a new row",
dateInput(paste0("Date_add", input$Add_row_head), "Date:", value = Sys.Date()),
numericInput(paste0("ReportNo_add", input$Add_row_head), "Report Number:",0),
selectInput(paste0("Supplier_add", input$Add_row_head), "Supplier Name:",choices=c("",mylist1)),
'''
Also, I do not know if it possible to read that field from the datatable itself instead of the csv file. Thank you in advance and hope to find a solution.

Related

How to add checkBox for variables group with tabPanel in R shiny

I need help in adding variables checkbox in r shiny within the tabPanel. I have already developed an r shiny app, in which on one of the page the data is displayed on the basis of some filterers and keywords search box.
So, I want to include one checkbox group of variables for the uploaded file.
I saw some of the solution but, non of them is based on tabPanel. Is it possible to have variable checkBox with tabPanel and how to place this under UI and SERVER. Also, as rest of the development is done so, would like to have solution with tabPanel if possible. Thanks
Tried adding the checkBoxInput but with not working with tabPanel and disturbing the current tabPanel
Below is my UI and SERVER
Here, the complete code is not displayed but, with following ui and server, wanted to add checkBoxInputgroup of variable and it should take the variable values from dfcase().
ui <- tabPanel('Evidence Finder in Background Text',
selectizeInput("Words1",
label="Important Words - Dictionary",
choices= TRUE,
multiple = TRUE,
options = list(maxOptions = 20000)),
DT::dataTableOutput("table2")
)
output$table2 <- DT::renderDataTable(server = FALSE,{
dfcase_bckg = as.data.frame(dfcase())
DT::datatable(dfcase_bckg,
rownames = FALSE,
escape = TRUE,
class = 'cell-border stripe',
selection = "single",
extensions = 'Buttons',
)
}
)
This is the view of the app and to left side i need the list of variables with checkbox

Write.csv using Action buttons

So basically I am trying to make a small setup of sorts and once a certain analysis is done, I would want to export a certain dataset generated to a predefined location and a predefined name (based on the inputs selected earlier). For this purpose, I used the action button which when clicked does this,
observeEvent(input$export_button, {
write.csv(input_dummy_data4ads,paste0("Dummy Files/",unique(input_dummy_data4ads$Dependent_Variable),"_", unique(input_dummy_data4ads$Model_Type),"_", unique(input_dummy_data4ads$AGM),".csv"),row.names = F,na="")
})
The issue here is that if I click the action button once, it generates the desired csv file and at the desired location too. But after pressing it once, it takes the value of 1 (input$export_button) so when I select a new set of inputs using the radio buttons and generate a new plot based on that (by clicking another action button), the app saves a new csv file with a new name (based on the new inputs) at the desired location. What I am trying to do is to reset the value of the action button so that the new csv file is created only when I click it every time.
I tried to understand this but could not incorporate it https://github.com/rstudio/shiny/issues/167
There are specific functions in shiny for this, use downloadButton in your ui and downloadHandler in server.
server.R:
output$export_data <- downloadHandler(
filename = function() {
paste0("Dummy Files/", unique(input_dummy_data4ads$Dependent_Variable), "_", unique(input_dummy_data4ads$Model_Type), "_", unique(input_dummy_data4ads$AGM), ".csv")
},
content = function(con) {
write.csv(input_dummy_data4ads, con, row.names = F, na = "")
}
)
ui.R:
downloadButton("export_data", "Export")

Shiny: Conflict with Pop Up Message and Data Table

I am creating an app with a form for users to fill in with details, and press a "Submit" button: after which a row gets added to a data frame summarising the entered data. Each entry has a unique identifier, eg. Name.
If a new submission is made, but references the same identifier, I want a pop-up box to warn the user that they are about to overwrite the original data.
Using information taken from this post, I have partially managed the aim. The code performs the update as expected (in running the example below, this is evidenced in the print() command), however the ui does not update as I'd expect.
Below I have included a minimal working example, where if one enters (for example) b into the "Row Name:" field, 10 into the "New Value:" field, and then click "Assign New Value", then the pop up box appears but the data table above does not change, moreover it appears to change shade. Then if you repeat with a second command, eg. b, 8, "Assign new Value", then the formatting goes back to normal, and both commits are seen to have taken affect.
I'd greatly appreciate if someone could explain why this is happening, and how to get the app to function as one would expect (eg. updating the table after first button click).
Moreover, if anybody has an idea of how I can extend this to accept/reject the update, that'd be great! By this I mean, having the option in the pop-up box to have "Are you sure you want to update row b?", and the options Yes/No.
Note whilst in the example below I have used the solution using shinyjs::alert (see comments in the above referenced post), I previously tried using the method outlined in the bulk of the post but had the same issue.
Thanks
library(shiny)
library(shinyjs)
library(DT)
ui <- fluidPage(
useShinyjs(),
dataTableOutput("DF_table"),
hr(),
fluidRow(
column(4,
textInput("rowName", "Row Name:", NULL) ),
column(4,
numericInput("newValue", "New Value:",NULL) ),
column(4,
actionButton("assignValue", label = h5("Assign New Value"), width = "100%" ) )
)
)
server <- function(input, output, session) {
rvs <- reactiveValues( DF = data.frame(name = c("a", "b", "c"), value = 1:3 ) )
observeEvent(input$assignValue,{
# Test if the supplied row name corresponds to a row of DF.
if(input$rowName %in% rvs$DF[,"name"] ){
# If it does, pop up box warns user that the supplied row is being over written.
shinyjs::alert(paste("Reassigning value of", input$rowName, sep=" ") )
# Over writes the value in the selected row, with the new value.
rvs$DF[match(input$rowName, rvs$DF[,"name"]), "value"] <- input$newValue
print(rvs$DF)
}
})
# Output data table.
output$DF_table <- renderDataTable(rvs$DF, rownames = FALSE)
}
runApp(list(ui = ui, server = server))

In R tcltk, how to check whether a widget exists and replace it if it does?

I'm working on a tcltk interface in R that creates a set of drop down combo boxes based on the fields in whichever dataset is loaded by the user. Right now I have a button ("getbox") to press after loading the data that reads the data then creates a combo box with an option for each field. I'd like to be able to re-create the combo box each time I press the button instead of adding a new combo box. Here is an example of what I have:
tt <- tktoplevel()
comboframe <- tkframe(tt)
getbox <- tkbutton(tt, text = "Create Combo Box", command = function() {
fields <- names(mtcars)
cbox <- tkwidget(comboframe, "ComboBox", editable = FALSE, values = fields)
tkgrid(cbox)
})
tkgrid(getbox)
tkgrid(comboframe)
I tried adding an if statement in the getbox command function, but couldn't get it to work.
if (exists(comboframe)) {
tkdestroy(comboframe)
} # then create combobox ...
Any thoughts on how to replace the combo box instead of adding a new one? Thanks!

R: Insert table in template table using ReporteRs

I am in the process of making a dashboard. Basically, I am pulling out selected information from a database that I then want to display in a word template. I set up the template with a table (2 rows, 3 columns). In each cell I put a bookmark. Then, using the R package ReporteRs I put specific information from R into a cell within the template based on the bookmark.
ISSUE: I cant seem to insert tables into a cell of the template.
Tried: I thought it might be due to size of the table (as i experienced that size was the issue when inserting figures), but this is not the case.
Below is an example with the error. In order to run this you have to create a Word document with a table with a bookmark in one of the cells called 'test'.
doc = docx( title = "Dashboard",template="H:\\test.docx")
myt <-FlexTable(cars[c(1:10),])
doc <-addFlexTable(doc,myt,bookmark="test")
writeDoc(doc, "H:\\testresult.docx")
If you then create a bookmark outside of the table and assign the table to the new bookmark, it seems to work.
Does anyone know how to insert a table (data frame as a table) into any cell of my template?
If argument bookmark is used, content (plots, paragraphs or images) will replace the whole paragraph containing the bookmark.
Behavior is different for tables: tables are added after the paragraph that contains the bookmark. A workaround is to add a paragraph after the bookmarked paragraph in the cell of the template. Then use deleteBookmark to delete the paragraph containing the bookmark.
doc <- docx( title = "Dashboard",template="H:\\test.docx")
myt <- FlexTable(cars[c(1:10),])
# make sure there is one new paragraph after the paragraph that contains 'test'
# add the FlexTable just after the paragraph containing bookmark 'test'
doc <- addFlexTable(doc,myt,bookmark="test")
# then delete the paragraph containing bookmark 'test'
deleteBookmark(doc, bookmark= "test")
writeDoc(doc, "H:\\testresult.docx")
You may use Sections with columns instead (and eventually column breaks):
doc = docx( )
doc = addSection(doc, landscape = TRUE, ncol = 2 )
doc = addPlot( doc = doc, fun = function() {
barplot( 1:8, col = 1:8 )
}, width = 3, height = 3, pointsize = 5)
doc = addColumnBreak(doc )
doc = addFlexTable(doc, FlexTable(head(iris) ) )

Resources